<?php
namespace Cyfy\Modules\Articles\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms;
use \Cyfy\Modules\Articles\Module as Articles;
use \Cyfy\Modules\Image\Image;
use \Cyfy\Modules\Locale\Locale;
use \Cyfy\Modules\BasicContent\Content;

class NewArticle extends Forms\Form
{
    private $path = ""; 
    
	function structure()
	{
        $this -> permission("Articles", "write and edit");
        $this -> action = "articles";
        

        
		$i = new Forms\TextBox("title", self :: t(10));
		$i -> required = true;
		$i -> maxlength = 255;
		$this -> addItem($i);
		
       
	
        $i = new Forms\TextBox("time", self :: t(20));
        $i -> format = "datetime";
		$i -> required = true;
		$i -> maxlength = 100;
		$this -> addItem($i);
        
        $i = new Forms\File("image2", self :: t(30), \Cyfy\Cyfy :: $filesDir . Articles :: IMAGE_ORIGINAL_DIR, 0, 0, array("jpg", "png", "gif"));
        $i -> filetypesDescription = self :: t(40);
        $i -> maxFiles = 1;
        $i -> customFilenames = array(Date("Y-m-d_h_i_s") . "_" . uniqid());
        $this -> addItem($i);
		
        
                $i = new Forms\Select("sid", self :: t(70));
        $i -> data = Articles :: getSections();
        $this -> addItem($i);
        
         $i = new Forms\TextBox("path", self :: t(80));
		$i -> maxlength = 255;
        $i -> description = self :: t(90);
		$this -> addItem($i);
        
        
		$w = new Forms\WysiwygEditor("text", self :: t(50));
		$w -> required = true;
		$this -> addItem($w);
		
		$b = new Forms\Button(self :: t(60));
		$this -> addButton($b);		
    }
    
    function validate()
    {
        if($this -> get("path")){
            $path = $this -> get("path");        
        }else{
            $path = text2Code($this -> get("title"));
        }
        $dt = new \datetime($this -> get("time"));
        $this -> path = $dt -> format(Articles :: ARTICLE_PATH_PREFIX) . $path;
        
        
         if(!\Cyfy\Cyfy :: tryPath($this -> path))
        {
            \Cyfy\Message :: set(self :: t(51, array($this -> path)), \Cyfy\Message::ERROR);
            return false;
        }
        
        return true;
    }
  
	function submit()
	{  
		// creating content
        $c = new Content($this -> get("title"), $this -> get("text"));
		$id = $c -> save();

        // saving failed
		if(!$id){
			Message :: set(self :: t(201, array($this -> get("title"))), Message::ERROR);
            return false;
        }
        
		// saving OK
        
        // image
        if($this -> get("image2")){
            $filename = array_pop($this -> get("image2"));
            $h = Articles :: Constant("imageHeight");
            $w = Articles :: Constant("imageWidth");
            if($w && $h)
            {
                $i = new Image(\Cyfy\Cyfy :: $filesDir . Articles :: IMAGE_ORIGINAL_DIR . $filename);
                $i -> resize($w, $h);
                $i -> saveAs(\Cyfy\Cyfy :: $filesDir . Articles :: IMAGE_DIR . $filename);
                $i -> close();
            }
            else
            {
                copy(\Cyfy\Cyfy :: $filesDir . Articles :: IMAGE_ORIGINAL_DIR . $filename, \Cyfy\Cyfy :: $filesDir . Articles :: IMAGE_DIR . $filename);
            }
        }else{
            $filename = "";
        }
            
            
        // add
        $short = substr(strip_tags($this -> get("text")), 0, Articles :: CROP_TEXT_TO);
        
        $v = array(
            'bcid' => $id,
            'time' => $this -> get("time"),
            'title' => $this -> get("title"),
            'sid' => $this -> get("sid"),
            'short' => $short,
            'path' => $this -> path,
            'image' => $filename
        );
            
        \dibi :: query("INSERT INTO [:cyfy:Articles-articles] %v", $v);
            
        // show message
        Message :: set(self :: t(81, array($this -> get("title"))));
        return true;
	}
}